import { defineStore } from 'pinia'
import router from '@/router/index'

export const useTabsStore = defineStore('tabs', {
	state: () => ({
		tabList: [{
			title: '导航页',
			path: "/admin-home",
			name: 'admin-home',
		}] as any[],
	}),
	actions: {
		// 关闭单个标签
		closeTab(tabIndex: any, curRoute: any) {
			const delItem = this.tabList.splice(tabIndex, 1)[0];
			const item = this.tabList[tabIndex]
				? this.tabList[tabIndex]
				: this.tabList[tabIndex - 1];
			if (item) {
				console.log('item', item)
				console.log('delItem.name', delItem.name)
				console.log('curRoute.name', curRoute.name)
				if (delItem.name === curRoute.name) {
					router.push(item.path)
				}
			} else {
				router.push("/");
			}
		},
		// 关闭所有标签
		closeAllTabs() {
			this.tabList = [{
				title: '导航页',
				path: "/Dashboard",
				name: 'Dashboard',
			}]
			router.push("/");
		},
		// 关闭其他标签
		closeOtherTab(curRoute: any) {
			const curItem = this.tabList.filter((item) => item.path === curRoute.fullPath);
			this.tabList = curItem;
		},
		// 添加标签
		addTab(route: any) {
			const isExist = this.tabList.some((item) => item.path === route.fullPath);
			if (!isExist) {
				this.tabList.push({
					title: route.meta.title,
					path: route.fullPath,
					name: route.name,
				});
			}
		}
	}
})